package com.lin.controller.square;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.base.Result;
import com.lin.entity.*;
import com.lin.security.util.SecurityUtil;
import com.lin.service.*;
import com.lin.util.JudgeIsMemberUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Created by Lin Weihong
 * @date on 2022/4/16 21:14
 */
@RestController
@RequestMapping("/api/square")
public class SquareController {

    @Resource
    UserService userService;

    @Resource
    TalentsService talentsService;

    @Resource
    ProjectOutsideService projectOutsideService;

    @Resource
    ProjectDetailService projectDetailService;

    @Resource
    TalentJoinService talentJoinService;

    @Resource
    ProjectInviteTalentsService projectInviteTalentsService;

    @Resource
    GroupInformationService groupInformationService;

    @GetMapping("/talents")
    public Result talentsList() {
        return Result.ok(talentsService.list(), "查询成功");
    }

    @GetMapping("/projects")
    public Result projectList() {
        List<ProjectOutside> projectOutsideList = projectOutsideService.list();
        projectOutsideList.forEach(p -> {
            String talentName = talentsService.getById(p.getMasterId()).getTalentName();
            p.setMasterName(talentName);
        });
        return Result.ok(projectOutsideList, "查询成功");
    }

    @GetMapping("/project_detail")
    public Result projectDetail(@RequestParam String projectId) {
        ProjectDetail projectDetail = projectDetailService.getById(projectId);
        if (projectDetail != null) {

            return Result.ok(projectDetail, "查询成功");
        }
        return Result.fail("此项目不存在！");
    }

    @PostMapping("/join")
    public Result joinProject(@RequestBody TalentJoin talentJoin) {
        try {
            talentJoinService.joinProject(talentJoin);
            return Result.ok("申请成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    //要判断项目Id是否存在！还没处理
    @PostMapping("/invite")
    public Result invite(@RequestBody @Validated ProjectInviteTalents projectInviteTalents) {
        Integer gId = projectDetailService.getById(projectInviteTalents.getProjectId()).getGroupId();
        if (userService.getById(projectInviteTalents.getUserId()) != null) { //判断系统中是否有此用户,如果不等于null，说明有这个用户
            if (!JudgeIsMemberUtil.judgeMemberIsInGroup(groupInformationService.getById(gId), projectInviteTalents.getUserId())) {//不在组内
                if (projectInviteTalentsService.getOne(new QueryWrapper<ProjectInviteTalents>()
                        .eq("project_id", projectInviteTalents.getProjectId())
                        .eq("user_id", projectInviteTalents.getUserId())) == null) {
                    Integer groupId = projectDetailService.getById(projectInviteTalents.getProjectId()).getGroupId();
                    projectInviteTalents.setGroupId(groupId);
                    projectInviteTalentsService.save(projectInviteTalents);
                } else {
                    return Result.fail("请不要重复邀请！");
                }
            }else {
                return Result.fail("该用户,已经在组内!");
            }
            return Result.ok("邀请成功");
        } else {
            return Result.fail("此用户不存在！");
        }


    }
}
